package com.taotao.delayjob.core.controller;


import com.taotao.delayjob.core.RetryTaskHelper;
import com.taotao.common.model.Pagination;
import com.taotao.common.model.RespData;
import com.taotao.delayjob.core.model.RetryInst;
import com.taotao.delayjob.core.model.RetryInstDto;
import com.taotao.delayjob.core.model.RetryInstPageData;
import com.taotao.delayjob.core.model.RetryInstPageParam;
import com.taotao.delayjob.core.model.RetryTriggerInfo;
import com.taotao.delayjob.core.service.RetryInstService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Set;

/**
 * <p>
 *
 * </p>
 *
 * @author fanbeibei
 * @version 1.0.0
 * @date 2024/7/17 18:50
 */
@RestController
@RequestMapping("/retry")
@Api(tags = "补偿任务")
@Slf4j
public class RetryController {

    @Autowired
    private RetryTaskHelper retryTaskHelper;

    @Autowired
    private RetryInstService retryInstService;


    @PostMapping("/param")
    @ApiOperation(value = "分页列表")
    public RespData<Pagination<RetryInstPageData>> queryPage(
            @ApiParam(value = "查询条件") @RequestBody RetryInstPageParam param,
            @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
        Pagination<RetryInstPageData> pageResult = retryInstService.queryPage(param, pageNum, pageSize);
        return new RespData<>(RespData.SUCCEED_CODE, null, pageResult);
    }


    @PostMapping("/resetRetryNumToZero")
    @ApiOperation(value = "重置已重试次数")
    public RespData<Object> resetRetryNumToZero(@RequestBody Set<Long> ids) {
        retryTaskHelper.resetRetryNumToZero(ids);
        return RespData.successResp();
    }


    @PostMapping("/add")
    @ApiOperation(value = "添加重试任务")
    public RespData<Object> add(@Validated @RequestBody RetryInstDto dto) {
        retryTaskHelper.commitRetryTask(dto);
        return RespData.successResp();
    }


    @PostMapping("/trigger")
    @ApiOperation(value = "手动触发")
    public RespData<Object> trigger(@Validated @RequestBody RetryTriggerInfo triggerInfo) {
        RetryInst r = retryInstService.getById(triggerInfo.getId());
        if (null == r) {
            return RespData.failedResp("id不存在");
        }

        if (null != triggerInfo.getParam() && !triggerInfo.getParam().isEmpty()) {
            r.setParam(triggerInfo.getParam());
        }

        retryTaskHelper.triggerRetry(r);
        return RespData.successResp();
    }

    @PostMapping("/rollback")
    @ApiOperation(value = "手动回滚")
    public RespData<Object> rollback(@Validated @RequestBody RetryTriggerInfo triggerInfo) {
        RetryInst r = retryInstService.getById(triggerInfo.getId());
        if (null == r) {
            return RespData.failedResp("id不存在");
        }

        if (null != triggerInfo.getParam() && !triggerInfo.getParam().isEmpty()) {
            r.setParam(triggerInfo.getParam());
        }

        retryTaskHelper.rollback(r);
        return RespData.successResp();
    }


}
